//
//  ZZCReadNewViewController.m
//  B段项目
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCReadNewViewController.h"
#import "ZZCReadTableViewCell.h"
#import "ZZCWebViewController.h"
@interface ZZCReadNewViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *DetailNewTableView;
@property (nonatomic, retain)NSMutableArray *detailNewArray;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, assign)NSInteger nextPage;
@property (nonatomic, retain)NSString *contentId;

@end

@implementation ZZCReadNewViewController

#pragma mark - 页面将要出现时候请求数据
-(void)viewWillAppear:(BOOL)animated{
    [self getAFNetWorkingData];
}



#pragma mark - viewDidload
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    // 创建tableView
    self.DetailNewTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStylePlain];
    self.DetailNewTableView.backgroundColor = [UIColor whiteColor];
    self.DetailNewTableView.delegate = self;
    self.DetailNewTableView.dataSource = self;
    [self.view addSubview:self.DetailNewTableView];
    
    // 注册cell
    [self.DetailNewTableView registerClass:[ZZCReadTableViewCell class] forCellReuseIdentifier:@"detailNewCell"];
    self.detailNewArray = [NSMutableArray array];
    
    // 调用上拉下拉方法
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    //设置初始值
    self.isUpLoad = NO;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(addHeaderRefresh)];
    self.DetailNewTableView.mj_header = header;
    [self.DetailNewTableView.mj_header beginRefreshing];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addFooterRefresh)];
    self.DetailNewTableView.mj_footer = footer;

}

#pragma mark - 设置下拉刷新
-(void)addHeaderRefresh
{
    self.nextPage = 0;// 记录页码的
    self.isUpLoad = NO;//标记为下拉操作
    // 重新请求数据
    [self getAFNetWorkingData];
}

#pragma mark - 设置上拉刷新
-(void)addFooterRefresh
{
    self.nextPage += 10;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getAFNetWorkingData];
}

#pragma mark - 数据解析
- (void)getAFNetWorkingData{
 
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"正在为您加载.....";
    [self.view addSubview:hub];
    
    NSString *urlStr = @"http://api2.pianke.me/read/columns_detail";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&limit=10&sort=addtime&start=%ld&typeid=%@&version=3.0.6", self.nextPage, self.readNewModel.type];
    
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.detailNewArray removeAllObjects];
        }
        
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            ZZCReadDetailModel *detail = [[ZZCReadDetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.detailNewArray addObject:detail];
        }
        
        [self.DetailNewTableView.mj_header endRefreshing];
        [self.DetailNewTableView.mj_footer endRefreshing];
        [self.DetailNewTableView reloadData];
        
        [hub hide:YES];
        
        
    } failure:^(NSError *error) {
        
        [hub hide:YES];
    }];
    
    
    
}


#pragma mark - 设置行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.detailNewArray.count;
}



#pragma mark - 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *detailNewIdntifier = @"detailNewCell";
    ZZCReadTableViewCell *detailNewCell = [tableView dequeueReusableCellWithIdentifier:detailNewIdntifier];
    // 取消点击cell的效果
    detailNewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //传值
    detailNewCell.detailListModel = [self.detailNewArray objectAtIndex:indexPath.row];
    
    return detailNewCell;
}


#pragma mark - 点击cell触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZZCWebViewController *detailVC = [[ZZCWebViewController alloc] init];
    ZZCReadDetailModel *detailList = [self.detailNewArray objectAtIndex:indexPath.row];
    detailVC.contentId = detailList.contentId;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



#pragma mark - 设定cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 自适应高度
    
    return HEIGHT/4;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
